from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin


MAX_LABEL_LENGTH = 100
MAX_TITLE_LENGTH = 100

class Stamp(models.Model):
    label = models.CharField(max_length=MAX_LABEL_LENGTH, db_index=True)
    root = models.ForeignKey('self', db_index=True, null=True, blank=True, default=None, related_name='descendant')
    parent = models.ForeignKey('self', db_index=True, null=True, blank=True, default=None, related_name='children')
    
    def __unicode__(self):
        return self.label
admin.site.register(Stamp)
    
class Serial(models.Model):
    title = models.CharField(max_length=MAX_TITLE_LENGTH, db_index=True)
    boards = models.ManyToManyField(Stamp)
    
    def __unicode__(self):
        return self.title
admin.site.register(Serial)
    
class Artical(models.Model):
    title = models.CharField(max_length=MAX_TITLE_LENGTH, db_index=True)
    content = models.TextField()
    
    parent = models.ForeignKey('self', null=True, db_index=True, blank=True, default=None) 
    type = models.CharField(max_length=8, db_index=True, default='.txt')
    create_time = models.DateTimeField(auto_now_add=True, db_index=True)
    modify_time = models.DateTimeField(auto_now=True, db_index=True)
    author = models.ForeignKey(User, db_index=True)
    serial = models.ForeignKey(Serial, db_index=True)
    
    def __unicode__(self):
        return self.title
admin.site.register(Artical)    
    
class CommentOfArtical(models.Model):
    artical = models.ForeignKey(Artical)
    comment = models.ForeignKey(Stamp)
    author = models.ForeignKey(User, db_index=True)
    commit_time = models.DateTimeField(auto_now_add=True)
    
    def __unicode__(self):
        return unicode(self.comment)
admin.site.register(CommentOfArtical)

